package com.tanhua.dubbo.server.api;

import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * 评论列表翻页
 *
 * @Author: zxw
 * @Description:
 * @Date: Created in 20:14 2021/1/17
 * @ModifiedBy:
 */
public class CommentImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public PageInfo<Comment> queryCommentsList(Integer messageID, Integer page,
                                               Integer pageSize, String sortProp, String sortOrder) {

        //构建分页对象
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize,
                Sort.by("descending".equals(sortOrder) ? Sort.Order.desc(sortProp) : Sort.Order.asc(sortProp)));

        //查询条件
        Query query = new Query(Criteria.where("publishId").is(messageID)).with(pageRequest);
        List<Comment> commentList = this.mongoTemplate.find(query, Comment.class);

        PageInfo<Comment> pageInfo = new PageInfo<>();
        pageInfo.setTotal(0);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(page);
        pageInfo.setRecords(commentList);

        return pageInfo;
    }
}
